#include "chooselevelsense.h"
#include "mainscene.h"
#include <QPixmap>
#include <QPainter>
#include <QMenuBar>
#include <QAction>
#include <QDebug>
#include <QTimer>
#include <QLabel>
#include <QSound>
#include "mypushbutton.h"
ChooseLevelSense::ChooseLevelSense(QWidget *parent) : QMainWindow(parent)
{
    //配置基础信息
    //设置固定大小
    this->setFixedSize(400,588);
    //设置图标
    this->setWindowIcon(QIcon(":/Coin0001.png"));
    //设置标题
    this->setWindowTitle("选择关卡！");
    //添加菜单栏  并且添加退出选项
    QMenuBar* bar=menuBar();
    setMenuBar(bar);
    QMenu *startMenu=bar->addMenu("开始");
    QAction *quitAction=startMenu->addAction("退出");
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });
    //添加返回按钮
    MyPushButton *backBtn=new MyPushButton(":/BackButton.png",":/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());
    //设置返回按钮的音乐
    QSound *backSound=new QSound(":/BackButtonSound.wav",this);
    //下面被注释掉的写法错误，两种类不能互相包含
    //mainScene=new MainScene;
    connect(backBtn,&QPushButton::clicked,[=](){
        //监听主场景的返回
        backSound->play();
        QTimer::singleShot(500,this,[=](){emit this->backSig();});        
    });
    for(int i=0;i<20;i++){
        MyPushButton *menuBtn=new MyPushButton(":/LevelIcon.png");
        menuBtn->setParent(this);
        //menuBtn->setText("1");
        menuBtn->move(70+i%4*70,130+i/4*70);
        connect(menuBtn,&MyPushButton::clicked,[=](){
            //进入到游戏场景
            //将选关场景屏蔽掉，并且开启游戏场景
            play=new PlayScene(i+1);
            this->hide();
            play->show();
            connect(play,&PlayScene::backSig,[=](){
                play->hide();
                this->show();
            });
        });
        //通过QLabel给每个标签添加文字
        QLabel *label=new QLabel;
        label->setParent(this);
        label->setFixedSize(menuBtn->width(),menuBtn->height());
        label->setText(QString::number(i+1));
        //设置文字的居中放置
        label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        label->move(70+i%4*70,130+i/4*70);
        //设置label 对鼠标操作透明  不然label对拦截原本用于点击button的事件
        //51号属性
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
    }
}

void ChooseLevelSense::paintEvent(QPaintEvent* ev)
{
    //加载背景图片
    QPixmap pix;
    pix.load(":/OtherSceneBg.png");
    QPainter painter(this);
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    //加载标题图片
    pix.load(":/Title.png");
    painter.drawPixmap((this->width()-pix.width())*0.5,30,pix.width(),pix.height(),pix);
}
